<template>
  <div>
    <div
      v-for="item in artists"
      @click="toArtistMsg(item.id)"
      class="each-artist"
      :key="item.id"
    >
      <div class="each-artist-pic">
        <img v-lazy="item.img1v1Url" alt="" />
      </div>
      <div class="each-artist-name">
        {{ item.name }}
      </div>
    </div>
    <mu-infinite-scroll
      :loadingText="loadingText"
      :loading="loading"
      @load="fetchData"
    />
  </div>
</template>

<script lang="ts">
import { APIType } from "@/api";
import { CommonIdType } from "@/model/id-or-enum.model";
import { Artist } from "@/model/music-info.model";
import { SearchType } from "@/model/search-response.model";
import { Vue, Component, Watch } from "vue-property-decorator";
import Router, { Route } from "vue-router";
@Component({})
export default class SearchResultArtistComponent extends Vue {
  artists: Artist[];
  loading = false;
  loadingText = "努力加载中...";

  $route: Route;
  $router: Router;
  $api: APIType;

  get query() {
    return this.$route.query.val;
  }
  @Watch("query")
  private onQueryChange(value: string, prev: string) {
    this.artists = [];
    this.fetchData();
  }

  mounted() {
    this.fetchData();
  }
  fetchData() {
    this.loading = true;
    const offset = this.artists.length;
    this.$api
      .searchByType<"Artist">(this.$route.query.val, SearchType.Artist, offset)
      .then((res) => {
        this.artists = [...this.artists, ...(res?.result?.artists ?? [])];
      })
      .finally(() => (this.loading = false));
  }
  toArtistMsg(id: CommonIdType) {
    this.$router.push({ path: `/artist/${id}` }).catch((err) => err);
  }
}
</script>

<style lang="scss" scoped>
.each-artist {
  display: flex;
  .each-artist-pic {
    width: 1rem;
    padding: 0.1rem;
    img {
      width: 100%;
    }
  }
  .each-artist-name {
    margin-left: 0.1rem;
    flex: 1;
    line-height: 1rem;
    border-bottom: 1px solid #f9fafc;
  }
}
</style>
